using System;
using System.Security.Cryptography;
using System.Text;

namespace OS.Common.Encrypt
{
	/// <summary>
	/// HMACSHA1 加密类
	/// </summary>
	[Obsolete]
	public static class HmacSha1
	{
		/// <summary>
		/// 返回加密后的
		/// </summary>
		/// <param name="data"></param>
		/// <param name="key"></param>
		/// <param name="encoding">如果为空，则默认Utf-8</param>
		/// <returns> 解密后的字节流通过Base64转化 </returns>
		public static string EncryptBase64(string data, string key, Encoding encoding = null)
		{
			if (encoding == null)
			{
				encoding = Encoding.UTF8;
			}
			byte[] bytes = encoding.GetBytes(data);
			return Convert.ToBase64String(Encrypt(encoding.GetBytes(key), bytes));
		}

		/// <summary>
		/// 返回加密后的
		/// </summary>
		/// <param name="data"></param>
		/// <param name="key"></param>
		/// <param name="encoding">如果为空，则默认Utf-8</param>
		/// <returns></returns>
		public static string Encrypt(string data, string key, Encoding encoding = null)
		{
			if (encoding == null)
			{
				encoding = Encoding.UTF8;
			}
			byte[] bytes = encoding.GetBytes(data);
			byte[] bytes2 = Encrypt(encoding.GetBytes(key), bytes);
			return encoding.GetString(bytes2);
		}

		/// <summary>
		/// 加密
		/// </summary>
		/// <param name="key"></param>
		/// <param name="bytes"></param>
		/// <returns></returns>
		private static byte[] Encrypt(byte[] key, byte[] bytes)
		{
			using (HMACSHA1 hMacsha = new HMACSHA1(key))
			{
				return hMacsha.ComputeHash(bytes);
			}
		}
	}
}
